package com.example.demo.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Author;
import com.example.demo.entity.BackResponseEntity;
import com.example.demo.entity.BooksInformation;
import com.example.demo.entity.requestJson.AHomepageQueryReq;
import com.example.demo.entity.responseJson.AHomeFuzzyqueryAuthorRes;
import com.example.demo.entity.responseJson.BooknameFussyQUeryRes;
import com.example.demo.entity.responseJson.HomePageBInformatiomRres;
import com.example.demo.entity.responseJson.HomePageBMoreInfoRes;
import com.example.demo.enums.ErrCodeEnum;
import com.example.demo.mapper.AuthorMapper;
import com.example.demo.mapper.BooksInformationMapper;
import com.example.demo.service.AHomepageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author : 杨丽辉
 * @date : 2021/3/6 9:58
 * @description:
 */
@Service
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class AHomepageServiceImpl implements AHomepageService {
    @Autowired
    private AuthorMapper authorMapper;
    @Autowired
    private BooksInformationMapper booksInformationMapper;

    /**
     * 主页模糊查询作者名
     * */
    @Override
    public BackResponseEntity<List<AHomeFuzzyqueryAuthorRes>> fuzzyQueryAuthor(AHomepageQueryReq aHomepageQueryReq) {
        String condition = null;

        if (!ObjectUtils.isEmpty(aHomepageQueryReq.getCondition())) {
            condition = aHomepageQueryReq.getCondition()
                    .trim();
        }
        if (ObjectUtils.isEmpty(condition)) {
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "查询条件不能为空");
        }

        LambdaQueryWrapper<Author> wrapper = new QueryWrapper<Author>().lambda();
        wrapper.select(Author::getAuthorNickname,Author::getAuthorId)
                .like(!ObjectUtils.isEmpty(condition), Author::getAuthorNickname, condition);

        List<Author> list = authorMapper.selectList(wrapper);
        List presenter = list;
        List<AHomeFuzzyqueryAuthorRes> res1 = presenter;
        return BackResponseEntity.success(ErrCodeEnum.unknown_0, res1, "");
    }



    /**
     * 模糊查询标签或一句话介绍
     * */
    @Override
    public BackResponseEntity<List<HomePageBMoreInfoRes>> queryLabels(AHomepageQueryReq aHomepageQueryReq){
        String condition = null;

        if(!ObjectUtils.isEmpty(aHomepageQueryReq.getCondition())){
            condition = aHomepageQueryReq.getCondition()
                    .trim();
        }
        if(ObjectUtils.isEmpty(condition)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }

        LambdaQueryWrapper<BooksInformation> queryWrapper = new QueryWrapper<BooksInformation>().lambda();
        queryWrapper.select(BooksInformation::getBooksId,BooksInformation::getBookName,BooksInformation::getAuthorName,BooksInformation::getBookPic,
                BooksInformation::getBookOnelabel,BooksInformation::getBookTwolabel,BooksInformation::getOneWord,BooksInformation::getContentCount)
                .like(!ObjectUtils.isEmpty(condition),BooksInformation::getOneWord,condition)
                .or().like(!ObjectUtils.isEmpty(condition),BooksInformation::getBookOnelabel,condition)
                .or().like(!ObjectUtils.isEmpty(condition),BooksInformation::getBookTwolabel,condition);

        List<BooksInformation> list = booksInformationMapper.selectList(queryWrapper);
        List presenter = list;
        List<HomePageBMoreInfoRes> res1 =presenter;

        return BackResponseEntity.success(ErrCodeEnum.unknown_0, res1, "");

    }

    /**
     * 模糊查询书名搜索框下面出现的选项
     * */
    @Override
    public BackResponseEntity<List<BooknameFussyQUeryRes>> fuzzyQueryBookname(AHomepageQueryReq aHomepageQueryReq){
        String condition = null;

        if(!ObjectUtils.isEmpty(aHomepageQueryReq.getCondition())){
            condition = aHomepageQueryReq.getCondition()
                    .trim();
        }
        if(ObjectUtils.isEmpty(condition)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_0, "搜索条件不能为空");
        }

        LambdaQueryWrapper<BooksInformation> wrapper = new QueryWrapper<BooksInformation>().lambda();
        wrapper.select(BooksInformation::getBooksId,BooksInformation::getBookName,BooksInformation::getAuthorName)
                .like(!ObjectUtils.isEmpty(condition), BooksInformation::getBookName, condition);

        List<BooksInformation> list = booksInformationMapper.selectList(wrapper);
        List presenter = list;
        List<BooknameFussyQUeryRes> res1 = presenter;
        return BackResponseEntity.success(ErrCodeEnum.unknown_0, res1, "");

    }

    /**
     * 模糊查询书名点击搜索之后出现在主页的搜索结果
     * */
    @Override
   public BackResponseEntity<List<HomePageBInformatiomRres>> queryHomepageBookname(AHomepageQueryReq aHomepageQueryReq){
        String condition = null;

        if(!ObjectUtils.isEmpty(aHomepageQueryReq.getCondition())){
            condition = aHomepageQueryReq.getCondition()
                    .trim();
        }
        if(ObjectUtils.isEmpty(condition)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_0, "搜索条件不能为空");
        }

        LambdaQueryWrapper<BooksInformation> wrapper = new QueryWrapper<BooksInformation>().lambda();
        wrapper.select(BooksInformation::getBooksId,BooksInformation::getBookName,BooksInformation::getAuthorName,BooksInformation::getBookPic)
                .like(!ObjectUtils.isEmpty(condition), BooksInformation::getBookName, condition);

        List<BooksInformation> list = booksInformationMapper.selectList(wrapper);
        List presenter = list;
        List<HomePageBInformatiomRres> res1 = presenter;
        return BackResponseEntity.success(ErrCodeEnum.unknown_0, res1, "");
    }
}
